package com.qf.controller;/*
亲元宝 2019/12/9  21:20
/**
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 * .............................................
 *          写字楼里写字间，写字间里程序员；
 *          程序人员写程序，又拿程序换酒钱。
 *          酒醒只在网上坐，酒醉还来网下眠；
 *          酒醉酒醒日复日，网上网下年复年。
 *          但愿老死电脑间，不愿鞠躬老板前；
 *          奔驰宝马贵者趣，公交自行程序员。
 *          别人笑我忒疯癫，我笑自己命太贱；
 *          不见满街漂亮妹，哪个归得程序员？
 */

import com.github.pagehelper.PageInfo;
import com.qf.bean.Shop;
import com.qf.service.ShopService;
import com.qf.util.ShopExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class ShopController {
    @Autowired
    private ShopService shopService;

    /*@RequestMapping("/test")
    public String test1(Map<String,Object> map){
        *//*Shop shop = new Shop();
        shop.setCity("北京");
        shop.setBuyway("支付宝");
        shop.setAgreement("不知道");

        Shop shop1=new Shop();
        shop.setCity("天津");
        shop.setBuyway("微信");
        shop.setAgreement("不知道");

        HashMap<String,Shop> map1=new HashMap<>();
        map1.put("shop", shop);
        map1.put("shop1", shop1);
        map.put("map1", map1);*//*

        map.put("name", "元宝");
        //return  "test1";

        return "test1";
    }*/

    @RequestMapping("/findallshop")
    public String findAll(ModelMap map,@RequestParam(defaultValue = "1") Integer pageindex
                    ,@RequestParam(defaultValue = "") String shopname,@RequestParam(defaultValue = "") String level){
        PageInfo<Shop> shops = shopService.findAllShop(pageindex, 5,shopname,level);

        System.out.println(shops);
        map.addAttribute("shops", shops);

        map.addAttribute("shopname", shopname);
        map.addAttribute("level", level);

        return "shoplist";
    }

    @GetMapping("/addshop")
    public String adshop(){

        return "addshop";
    }

    @PostMapping("/addshop")
    public String addshop(Shop shop,HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        shop.setStatus(1);
        shop.setStagestate("解约");
        shop.setResultstatus(1);
        shop.setShopstatus("1");
        shop.setAgreement("合同是假的");
        shop.setProductinfo("产品是假的");
        System.out.println(shop);
        Integer user=shopService.insertShop(shop);
        if (user>0){
            writer.print("<script>alert('add successful 成功');location.href='/findallshop'</script>");
        }else {
            writer.print("<script>alert('add successful 失败');location.href='/findallshop'</script>");
        }
        return null;
    }

    //假删除 仅仅修改状态
    @RequestMapping("/deleteshop")
    public String deleteshop(Integer sid, HttpServletResponse response) throws IOException {
        int i = shopService.deleteshopBysid(sid);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        if (i > 0) {

            writer.print("<script>alert('Delete successful 成功');location.href='/findallshop'</script>");
        } else {
            writer.print("<script>alert('Delete failed 失败');location.href='/findallshop'</script>");
        }
        return null;
    }
    //批量删除
    @RequestMapping("/deleteshops")
    public void deletes(int[] sid,HttpServletResponse response,String method) throws IOException {

        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        if (method.equals("delete")){
            int i = shopService.deletes(sid);
            if(i>0){
                writer.print("<script>alert('删除成功');location.href='/findallshop'</script>");
            }else{
                writer.print("<script>alert('删除失败');location.href='/findallshop'</script>");
            }
        }else if (method.equals("daochu")){
            //1.导出数据
            List<Shop> shopList=shopService.findbyids(sid);
            //2.导出数据
            ShopExcelUtils.createFirst("用户信息");
            ShopExcelUtils.createOthers(shopList);
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
            ShopExcelUtils.export(new FileOutputStream("D:\\atm//"+(format.format(new Date()))+".xls"));
            writer.print("<script>alert('导出成功');location.href='findallshop'</script>");
        }

    }

    //通过主键查询id
    @RequestMapping("/findbysid")
    public String updateshop(Integer sid,ModelMap map){
        Shop shop=shopService.findbysid(sid);
        map.addAttribute("shop", shop);

        return "shopupdate";
    }

    //修改
    @RequestMapping("/updateshop")
    public String updateshop(Shop shop,HttpServletResponse response) throws IOException {

        System.out.println("update shop"+shop);
        int i = shopService.updateShop(shop);
        System.out.println("i"+i);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        if (i > 0) {

            writer.print("<script>alert('Update successful 成功');location.href='/findallshop'</script>");
        } else {
            writer.print("<script>alert('Update failed 失败');location.href='/findallshop'</script>");
        }
        return null;
    }



















   /* @RequestMapping("/login.html")
    public String test(){
        return "login";
    }
    @RequestMapping("/head.html")
    public String test2(){
        return "head";
    }

    @RequestMapping("/index.html")
    public String test3(){
        return "index";
    }
    @RequestMapping("/left.html")
    public String test04(){
        return "left";
    }
    @RequestMapping("/right.html")
    public String test05(){
        return "right";
    }*/
   @RequestMapping("/update.html")
    public String test06(){
       return "shopupdate2";
   }


}
